// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-07-31

package db

import (
	"gitee.com/go-libs/db-xorm/db/src"
	"gitee.com/go-libs/db-xorm/db/types"
)

// Alias for xorm.

type (
	Session = src.Session
)

// Alias for src.

var (
	// Config
	// is an alias for src.Config. You can open a master or slave connect
	// session like follows.
	//
	// Master:
	//   session, err := db.Config.GetMaster(ctx)
	//
	// Slave:
	//   session, err := db.Config.GetSlave(ctx)
	Config = src.Config

	// Transaction
	// is an alias for src.Transaction.
	Transaction = src.Transaction

	// TransactionWithSession
	// is an alias for src.Transaction.
	TransactionWithSession = src.TransactionWithSession
)

type (
	// Database
	// is an alias for database connection configuration.
	//
	// File: configure in config/db.yaml like follows:
	//
	//   database:
	//     my:
	//       driver: mysql
	//       dsn:
	//         - user:pass@tcp(127.0.0.1)/name?charset=utf8
	//
	// Code: in your code like follows:
	//
	//    db.Config.Add("my", &db.Database{
	//        Driver: "mysql",
	//        Dsn: []string{
	//            "user:pass@tcp(127.0.0.1)/name?charset=utf8",
	//        },
	//    })
	//
	// Usage:
	//
	//   db.Config.GetMaster(ctx, "my")
	//   db.Config.GetSlave(ctx, "my")
	Database = src.Database

	// Service
	// is an alias for src.Service. It's a top level service. You can
	// import it as an anonymous.
	//
	// Definition:
	//
	//   type YourService struct {
	//       db.Service
	//   }
	//
	//   func NewYourService(sessions ... *db.Session) *YourService {
	//       service := &YourService{}
	//       service.With(sessions...)
	//       return service
	//   }
	//
	//   func (o *YourService) List(ctx context.Context)(list []models.Example, err error){
	//       var session *db.Session
	//
	//        if session, err = o.Slave(ctx); err != nil {
	//            return
	//        }
	//
	//        err = session.Find(&list)
	//        return
	//   }
	//
	// Usage:
	//
	//   svc := services.NewYourService()
	//   list, err := svc.List(ctx)
	Service = src.Service

	// TransactionHandler
	// is an alias hook for src.TransactionHandler. Do not run handler
	// in goroutine for each transaction.
	TransactionHandler = src.TransactionHandler
)

// Definitions.

var (
	// NewDate
	// is an alias for types.NewDate.
	NewDate = types.NewDate

	// NewDatetime
	// is an alias for types.NewDatetime.
	NewDatetime = types.NewDatetime

	SetLocation = types.SetLocation
)

// Now
// is an alias for src.Now. It's returned a Datetime type of
// current time.
func Now() types.Datetime {
	return NewDatetime()
}

type (
	// Date
	// is an alias for types.Date. It's a wrapper of time.Time.
	Date = types.Date

	// Datetime
	// is an alias for types.Datetime. It's a wrapper of time.Time.
	Datetime = types.Datetime
)
